package com.zd;


import cn.hutool.core.util.HexUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zd.dao.ReceiverData;
import com.zd.mapper.ReceiverDataMapper;
import com.zd.utils.MqttUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import javax.annotation.Resource;
import java.util.List;
import java.util.TimeZone;

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
@Slf4j
@RequiredArgsConstructor
public class BaseDevicePlatApplication implements CommandLineRunner {

    private final ReceiverDataMapper receiverDataMapper;

    @Resource(name = "dataThreadPool")
    private ThreadPoolTaskExecutor executor;


    public static void main(String[] args) {
        TimeZone.setDefault(TimeZone.getTimeZone("GMT+8"));
        SpringApplication.run(BaseDevicePlatApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        try{
            DynamicDataSourceContextHolder.push("sensorSource");
            List<ReceiverData> receiverData = receiverDataMapper.selectList(new LambdaQueryWrapper<ReceiverData>().eq(ReceiverData::getSn, "SN09433796").ge(ReceiverData::getDatetime, "2025-02-03 11:42:00").le(ReceiverData::getDatetime, "2025-02-08 18:30:00"));
            log.info("查询数据：{}", receiverData);
//            if (ObjectUtil.isNotNull(receiverData)) {
//                byte[] dataBytes = receiverData.toLocationByteArray(Integer.parseInt(address));
//                log.info("发送数据：{}", HexUtil.encodeHexStr(dataBytes));
//
//            executor.execute(() -> MqttUtil.SendDataByMqtt("tcp://124.71.85.152:1883", sendSn, "devicefrontparse", "devicefrontparse", sendSn + "/OTA", dataBytes));
//            }
        }catch (Exception e){
            log.error("发送失败", e);
        }finally {
            DynamicDataSourceContextHolder.poll();
        }
    }
}





